<template>
<ul class="ul">
    <li v-for="(item, index) in lists" :key="item[id]" class="li">
        <div class="tr" :data-cate-id="item.cat_id">
            <div 
                class="td" 
                :style="'transform: translateX('+ (item.cat_level+1) * 14 +'px);'"
            >{{item.cid || index + 1}}</div>
            <div class="td">
                <span 
                    v-if="item.children"
                    class="open-tree" 
                    :style="'transform: translateX('+ (item.cat_level+1) * 14 +'px);'" 
                    @click="handleClickOpenCateTree"
                ></span>
                <span
                    :style="'transform: translateX('+ (item.cat_level+1) * 14 +'px);'" 
                >{{item.cat_name}}</span>
            </div>
            <div class="td">
                <span class="round-true"></span>
            </div>
            <div class="td">
                <my-tag v-if="item.cat_level===0" :levle="1">一级</my-tag>
                <my-tag v-else-if="item.cat_level===1" :levle="2">二级</my-tag>
                <my-tag v-else :levle="3">三级</my-tag>
            </div>
            <div class="td">
                <slot>
                <my-button
                    :type="'edit'"
                    class="edit"
                >编辑</my-button>
                <my-button
                    :type="'delete'"
                    @click.native="handleClickDeleteCate"
                >删除</my-button>
                </slot>
            </div>
        </div>
        <tree-table 
            v-if="list && list.length > 0"
            v-show="String(item.cat_id) === validataCateId"
            :list="item.children" 
            :id="id" 
            @update-data-event='handleUpdateData'
        ></tree-table>
    </li>
</ul>
</template>

<script>
import { 
    deleteGoodsCate 
} from "../../config/data.js"
export default {
    name: "treeTable",
    props: {
        list: { /* 数据列表 */
            required: true,
            type: Array,
            default: function() {
                return [];
            }
        },
        id: { /* 属性（唯一） */
            type: String,
            default: "id"
        },
    },
    data() {
        return {
            validataCateId: ""
        }
    },
    methods: {
        handleUpdateData(data) {
            this.$emit('update-data-event', data)
        },
        async handleClickDeleteCate(e) {
            let result = await deleteGoodsCate({
                cid: e.target.parentNode.parentNode.dataset.cateId
            })
            if(result.fsBool) {
                this.validataCateId = ""
                this.$emit('update-data-event', result.data.tree.length > 0 ? true : false)
            }
        },
        handleClickOpenCateTree(e) {
            if(e.target.constructor === HTMLSpanElement) {
                const cid = e.target.parentNode.parentNode.dataset.cateId
                if(this.validataCateId === cid) {
                    this.validataCateId = ""
                    return;
                }
                this.validataCateId = cid
            }
        }
    },
    computed: {
        lists() {
            return this.list;
        }
    }
}
</script>

<style scoped>
.li {
    overflow: hidden;
    border-top: 1px solid #cecece;
}
.tr {
    display: flex;
    align-items: center;
}
.td {
    display: flex;
    align-items: center;
    width: 100%;
    height: 42px;
    border-left: 1px solid #cecece;
    box-sizing: border-box;
}
.td:nth-of-type(1) {
    min-width: 64px;
    max-width: 64px;
    width: 64px;
    border-left: none;
}
.td:nth-of-type(2) {
    overflow: hidden;
    white-space: nowrap;
    min-width: 160px;
    text-overflow: ellipsis;
}
.td:nth-of-type(3),
.td:nth-of-type(4) {
    justify-content: center;
    min-width: 80px;
}
.td:nth-of-type(5) {
    justify-content: center;
    min-width: 160px;
}
.open-tree {
    position: relative;
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 5px;
    border-radius: 50%;
    background-color: #ccc;
    cursor: pointer;
}
.open-tree::before,
.open-tree::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
}
.open-tree::before {
    width: 12px;
    height: 1px;
    margin-left: -6px;
    background-color: #999;
}
.open-tree::after {
    width: 1px;
    height: 12px;
    margin-top: -6px;
    background-color: #999;
}
.round-true {
    position: relative;
}
.round-true::after {
    content: "√";
    position: absolute;
    top: 50%;
    left: 50%;
    display: inline-block;
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-top: -10px;
    margin-left: -10px;
    padding-top: 3px;
    padding-left: 5px;
    border-radius: 50%;
    color: #fff;
    background-color: rgb(49, 223, 78);
    font-size: 14px;
    box-sizing: border-box;
}
.edit {
    margin-right: 12px;
}
</style>